package com.laikan.library.httpvolley.request;

import com.android.volley.*;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.laikan.library.components.BasicApplication;
import com.laikan.library.utils.AndroidUtils;
import com.laikan.library.utils.DeviceUtil;
import com.socks.library.KLog;

import java.util.HashMap;
import java.util.Map;

public class HttpRequest extends Request<String> {

    protected String url;
    private long startRequestTime = 0;// 开始发起请求的时间
    private long endRequestTime = 0; // 请求结束的时间
    private Map<String, String> requestHeaders;
    private Map<String, String> responseHeaders;
    private final Listener<String> mListener;

    HttpRequest(int method, String url, Listener<String> listener, ErrorListener errorListener) {

        super(method, url, errorListener);
        this.mListener = listener;
        this.url = url;
        setRetryPolicy(getRetryPolicy());
    }

    @Override
    protected void deliverResponse(String response) {

        mListener.onResponse(response);
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {

        responseHeaders = response.headers;// 所有的响应头
        String parsed;
        try {
            String charset = HttpHeaderParser.parseCharset(response.headers);
            KLog.d("encodeType:" + charset);
            byte[] bytes = DeviceUtil.gzipDecompress(response.data);
            parsed = new String(bytes, charset);
        }catch (Exception e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {

        if (requestHeaders == null || requestHeaders.size() == 0) {
            return super.getHeaders();
        }else {
            return requestHeaders;
        }
    }

    @Override
    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
        return super.setRetryPolicy(retryPolicy);
    }

    @Override
    public RetryPolicy getRetryPolicy() {

        int SOCKET_TIMEOUT = 20 * 1000;
        return new DefaultRetryPolicy(
            SOCKET_TIMEOUT,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    }

    private Map<String, String> getCommonHeader() {

        // 设置字符集为UTF-8,并采用gzip压缩传输
        Map<String, String> headers = new HashMap<>();
//        headers.put("Charset", "UTF-8");
        // headers.put("Content-Type", "application/json");
//        headers.put("Accept-Encoding", "gzip,deflate");
        headers.put("os", "android");
        headers.put("version", AndroidUtils.getCurrentAppVer(BasicApplication.getInstance()));
        return headers;
    }

    public String getUrl() {

        return url;
    }

    public void setUrl(String url) {

        this.url = url;
    }

    public long getStartRequestTime() {

        return startRequestTime;
    }

    public void setStartRequestTime(long startRequestTime) {

        this.startRequestTime = startRequestTime;
    }

    public long getEndRequestTime() {

        return endRequestTime;
    }

    public void setEndRequestTime(long endRequestTime) {

        this.endRequestTime = endRequestTime;
    }

    public void setHeaders(Map<String, String> headers) {

        requestHeaders = getCommonHeader();
        if (headers != null){
            requestHeaders.putAll(headers);
        }
    }

    public Map<String, String> getResponseHeaders() {

        return responseHeaders;
    }

    public Map<String, String> getRequestHeaders() {

        return requestHeaders;
    }
}
